/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EventFrame.java
 *
 * Created on 2010-maj-03, 17:17:41
 */

package foalvleditor.gui;

import foalvleditor.core.impl.LevelModel;
import foalvleditor.core.impl.SelectionModel;
import foalvleditor.gameobjects.GameEvent;
import foalvleditor.gameobjects.GameEvent.EventType;
import foalvleditor.gameobjects.AbstractGameObject;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 *
 * @author Mikael
 */
public class JNewEventFrame extends javax.swing.JFrame implements WindowFocusListener {

    /**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private final static LevelModel levelModel=LevelModel.getInstance();
    private static SelectionModel selectionModel=SelectionModel.getInstance();
    private final AbstractGameObject ago;

    /** Creates new form EventFrame */
    public JNewEventFrame(AbstractGameObject obj) {
        super();
        initComponents();
        this.addWindowFocusListener(this);
        this.ago=obj;
        try{
            AbstractGameObject thisGameObject;
            JSimpleObjectPanel newPanel;
        for (int i=0; i<levelModel.getLevel().size(); i++)
            {
            thisGameObject=levelModel.getLevel().get(i);
            newPanel=new JSimpleObjectPanel(thisGameObject);
            if (!thisGameObject.equals(ago)) objectPanel.add(newPanel);
            }
        }
        catch (Exception e){
            JOptionPane.showMessageDialog(null, "Error while processing events.");;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        objectPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(foalvleditor.core.FoaLvlEditorApp.class).getContext().getResourceMap(JNewEventFrame.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setAlwaysOnTop(true);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setFocusCycleRoot(false);
        setMinimumSize(new java.awt.Dimension(250, 300));
        setName("Form"); // NOI18N
        setResizable(false);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(foalvleditor.core.FoaLvlEditorApp.class).getContext().getActionMap(JNewEventFrame.class, this);
        jButton2.setAction(actionMap.get("okClick")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jButton3.setAction(actionMap.get("cancelClick")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        objectPanel.setName("objectPanel"); // NOI18N
        objectPanel.setLayout(new java.awt.GridLayout(4, 100));
        jScrollPane1.setViewportView(objectPanel);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setName("jLabel2"); // NOI18N

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Switch", "Chain" }));
        jComboBox1.setName("jComboBox1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2))
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
            }
        });
    }

    @SuppressWarnings("unchecked")
	@Action
    public void okClick() {
        if (selectionModel.getSelected().size()>0){
        EventType type=EventType.SWITCH;
        if (jComboBox1.getSelectedIndex()==1) type=EventType.CHAIN;
        ArrayList<AbstractGameObject> clone = (ArrayList<AbstractGameObject>)
		(selectionModel.getSelected()).clone();
		levelModel.addEvent(new GameEvent(type, ago,
                clone));
        selectionModel.deselectAll();
        }
        this.dispose();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel objectPanel;
    // End of variables declaration//GEN-END:variables

    public void windowLostFocus(WindowEvent e) {
        this.toFront();
        this.requestFocus();
    }

    @Action
    public void cancelClick() {
        this.dispose();
    }

    public void windowGainedFocus(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
